<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="零件名称" prop="vSpaName">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入零件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="vSpaModel">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="vSpaBrand">
        <el-input
          v-model="queryParams.vSpaBrand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购单号" prop="vBuyNo">
        <el-input
          v-model="queryParams.vBuyNo"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="采购单号" align="center" prop="vBuyNo" />
      <el-table-column label="零件名称" align="center" prop="vSpaName" />
      <el-table-column label="规格型号" align="center" prop="vSpaModel" />
      <el-table-column label="品牌" align="center" prop="vSpaBrand" />
      <el-table-column label="单位" align="center" prop="vUnit" />
      <el-table-column label="数量" align="center" prop="nQty" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="质保期" align="center" prop="nDays" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="指定人员" prop="oprBy">
          <el-select v-model="form.oprBy" placeholder="请选择" filterable>
            <el-option
              v-for="item in oprList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.value
              }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, getOprInfo, save } from "@/api/buy/buy0011";

export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单明细表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vSpaName: null,
        vSpaModel: null,
        vSpaBrand: null,
        vBuyNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        oprBy: [
          { required: true, trigger: "blur", message: "指派人员不能为空" },
        ],
      },
      clickRow: {},

      oprList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请购单明细列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleUpdate(row) {
      getOprInfo().then((response) => {
        this.oprList = response.data;
        this.clickRow = row;
        this.open = true;
      });
    },

    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.clickRow.oprBy = this.form.oprBy;
          save(this.clickRow).then((response) => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
